package datastructure.line.Stack;

import datastructure.line.Array.Array;

/**
 * 自定义栈
 * 栈是一种数据结构
 * 只能从数据的一端添加或取出元素---被称为栈顶
 */
public class ArrayStack<E> implements IStack<E> {
    private Array<E> array = new Array<E>();

    public ArrayStack() {
    }

    @Override
    public void push(E e) {
        array.addLast(e);
    }

    @Override
    public void pop() {
        array.remove(array.getSize() - 1);
    }

    @Override
    public E peek() {
        return array.get(array.getSize() - 1);
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return getSize() == 0;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Top:[");
        for (int i = 0; i < getSize(); i++) {
            E datum = array.get(i);
            sb.append(datum);
            sb.append(" ");
        }
        String trim = sb.toString().trim();
        trim += "] End";
        return trim;
    }
}
